#include "cellforindex.h"
//代表空间索引中的每一个网格单元

cellForIndex::cellForIndex()
{

}

bool cellForIndex::isPointWithin(QPointF pt)
{
    if(pt.rx()<maxX&&pt.rx()>minX&&pt.ry()<maxY&&pt.ry()>minY)
        return true;
    else return false;
}

bool cellForIndex::conductIndex(geoObject *object)
{
    if(object==nullptr)
    {
        qDebug()<<"目标物体为空。";
        return false;
    }
    if(object->type=="MultiPolygon")
    {
        foreach(geoPolygon* polygon,dynamic_cast<geoMultiPolygon* >(object)->polygons)
        {
            foreach(QPointF coordinate,polygon->projectedCoordinates)
            {
                if(isPointWithin(coordinate))
                {
                    objectsWithin.append(object);
                    return true;
                }
            }
        }
    }
    else if(object->type=="MultiLineString")
    {
        foreach(geoLineString* line,dynamic_cast<geoMultiLineString* >(object)->lineStrings)
        {
            foreach(QPointF coordinate,line->projectedCoordinates)
            {
                if(isPointWithin(coordinate))
                {
                    objectsWithin.append(object);
                    return true;
                }
            }
        }
    }
    else
    {
        foreach(QPointF coordinate,object->projectedCoordinates)
        {
            if(isPointWithin(coordinate))
            {
                objectsWithin.append(object);
                return true;
            }
        }
    }
    return false;
}

QVector<geoObject*> cellForIndex::getObjectWithin()
{
    return objectsWithin;
}

QVector<cellForIndex*> cellForIndex::getChildrenCell()
{
    return childrenCell;
}

bool cellForIndex::addChildCell(cellForIndex*cell)
{
    if(cell!=nullptr)
    {
        childrenCell.append(cell);
        return true;
    }
    else return false;
}

cellForIndex* cellForIndex::getChildCell(int i)
{
    if(i>=0&&i<childrenCell.size())
        return childrenCell[i];
    else return nullptr;
}

bool cellForIndex::setObjectsWithin(QVector<geoObject *> objects)
{
    this->objectsWithin=objects;
    return true;
}
